<?php
      header("Access-Control-Allow-Origin:*");
      header("Content-type:text/html;charset=utf-8");
      require "./connect_mySQL.php";

      $username = $_POST["username"];
      $password = $_POST["password"];
      $email = $_POST["email"];
      $author = $_POST["author"];

      if($username === "" || $password === ""){
            $json_arr = array( "status" => "error" , "stateCode" => "4" , "errorType" => "username an password is null");
            die(json_encode($json_arr));
      }
     
      if(!in_array($author, $tb_list)){
            $json_arr = array( "status" => "error" , "stateCode" => "4" , "errorType" => "author is error");
            die(json_encode($json_arr));
      }
      createTable($con,$author);
      // $json_arr = array( "status" => mysql_fetch_array($show_res));
      // die(json_encode($json_arr));

      // 查询用户名是否重复;   SELECT from 
      $select_SQL = "SELECT username FROM $author";
      $select_source = mysql_query($select_SQL,$con);
      if($select_source){
      // 拿出资源逐条进行检索;
            while($row = mysql_fetch_array($select_source)){
                  if($row["username"] === $username){ 
                        $json_arr = array( "status" => "error" , "stateCode" => "4" , "errorType" => "repeat of user name");
                        die(json_encode($json_arr));
                  } 
            } 
      }

      // 将内容放入数据库之中;  INSERT  INTO 
      // 食指长的SQL语句;

      $password = md5($password);
      $insert_SQL = "INSERT INTO $author (
            `username`,
            `password`,
            `email`
      )
            VALUES 
      (
            '$username',
            '$password',
            '$email'
      )
      ";
      // echo $insert_SQL;
      // 创建SQL语句结束之后使用mysql_query执行语句;

      $res = mysql_query($insert_SQL,$con);

      if(!$res){
            $json_arr = array( "status" => "error" , "stateCode" => "5" , "errorType" => "insert Error".mysql_error());
            die(json_encode($json_arr));
      }


      $json_arr = array( "status" => "success" , "stateCode" => "1" , "password" => $password);
      echo json_encode($json_arr);
      mysql_close($con);
?>